package com.transitionseverywhere;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Property;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.transitionseverywhere.utils.MatrixUtils;
import java.util.Map;

/* loaded from: classes.dex */
public class ChangeImageTransform extends Transition {
    private static final Property<ImageView, Matrix> ANIMATED_TRANSFORM_PROPERTY;
    private static final String TAG = "ChangeImageTransform";
    private static final String PROPNAME_MATRIX = "android:changeImageTransform:matrix";
    private static final String PROPNAME_BOUNDS = "android:changeImageTransform:bounds";
    private static final String[] sTransitionProperties = {PROPNAME_MATRIX, PROPNAME_BOUNDS};

    static {
        if (Build.VERSION.SDK_INT >= 14) {
            ANIMATED_TRANSFORM_PROPERTY = new Property<ImageView, Matrix>(Matrix.class, "animatedTransform") { // from class: com.transitionseverywhere.ChangeImageTransform.1
                @Override // android.util.Property
                public Matrix get(ImageView imageView) {
                    return null;
                }

                @Override // android.util.Property
                public void set(ImageView imageView, Matrix matrix) {
                    MatrixUtils.animateTransform(imageView, matrix);
                }
            };
        } else {
            ANIMATED_TRANSFORM_PROPERTY = null;
        }
    }

    public ChangeImageTransform() {
    }

    public ChangeImageTransform(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    private void captureValues(TransitionValues transitionValues) {
        ImageView imageView;
        Drawable drawable;
        Matrix matrix;
        Matrix matrix2;
        View view = transitionValues.view;
        if ((view instanceof ImageView) && view.getVisibility() == 0 && (drawable = (imageView = (ImageView) view).getDrawable()) != null) {
            Map<String, Object> map = transitionValues.values;
            Rect rect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
            map.put(PROPNAME_BOUNDS, rect);
            if (imageView.getScaleType() == ImageView.ScaleType.FIT_XY) {
                Matrix imageMatrix = imageView.getImageMatrix();
                if (!imageMatrix.isIdentity()) {
                    matrix2 = new Matrix(imageMatrix);
                } else {
                    int intrinsicWidth = drawable.getIntrinsicWidth();
                    int intrinsicHeight = drawable.getIntrinsicHeight();
                    if (intrinsicWidth <= 0 || intrinsicHeight <= 0) {
                        matrix = null;
                    } else {
                        float width = rect.width() / intrinsicWidth;
                        float height = rect.height() / intrinsicHeight;
                        matrix2 = new Matrix();
                        matrix2.setScale(width, height);
                    }
                }
                map.put(PROPNAME_MATRIX, matrix2);
            }
            matrix = new Matrix(imageView.getImageMatrix());
            matrix2 = matrix;
            map.put(PROPNAME_MATRIX, matrix2);
        }
    }

    @Override // com.transitionseverywhere.Transition
    public void captureStartValues(TransitionValues transitionValues) {
        captureValues(transitionValues);
    }

    @Override // com.transitionseverywhere.Transition
    public void captureEndValues(TransitionValues transitionValues) {
        captureValues(transitionValues);
    }

    @Override // com.transitionseverywhere.Transition
    public String[] getTransitionProperties() {
        return sTransitionProperties;
    }

    @Override // com.transitionseverywhere.Transition
    public Animator createAnimator(ViewGroup viewGroup, TransitionValues transitionValues, TransitionValues transitionValues2) {
        if (transitionValues == null || transitionValues2 == null) {
            return null;
        }
        Rect rect = (Rect) transitionValues.values.get(PROPNAME_BOUNDS);
        Rect rect2 = (Rect) transitionValues2.values.get(PROPNAME_BOUNDS);
        if (rect == null || rect2 == null) {
            return null;
        }
        Matrix matrix = (Matrix) transitionValues.values.get(PROPNAME_MATRIX);
        Matrix matrix2 = (Matrix) transitionValues2.values.get(PROPNAME_MATRIX);
        boolean z = (matrix == null && matrix2 == null) || (matrix != null && matrix.equals(matrix2));
        if (rect.equals(rect2) && z) {
            return null;
        }
        ImageView imageView = (ImageView) transitionValues2.view;
        Drawable drawable = imageView.getDrawable();
        int intrinsicWidth = drawable.getIntrinsicWidth();
        int intrinsicHeight = drawable.getIntrinsicHeight();
        if (intrinsicWidth == 0 || intrinsicHeight == 0) {
            return createMatrixAnimator(imageView, new MatrixUtils.NullMatrixEvaluator(), MatrixUtils.IDENTITY_MATRIX, MatrixUtils.IDENTITY_MATRIX);
        }
        if (matrix == null) {
            matrix = MatrixUtils.IDENTITY_MATRIX;
        }
        if (matrix2 == null) {
            matrix2 = MatrixUtils.IDENTITY_MATRIX;
        }
        MatrixUtils.animateTransform(imageView, matrix);
        return createMatrixAnimator(imageView, new MatrixUtils.MatrixEvaluator(), matrix, matrix2);
    }

    private ObjectAnimator createMatrixAnimator(ImageView imageView, TypeEvaluator<Matrix> typeEvaluator, Matrix matrix, Matrix matrix2) {
        return ObjectAnimator.ofObject(imageView, (Property<ImageView, V>) ANIMATED_TRANSFORM_PROPERTY, (TypeEvaluator) typeEvaluator, (Object[]) new Matrix[]{matrix, matrix2});
    }
}
